<?php
    
    // auto load classes
    function __autoload($className){
        $type = substr($className, 0, 1);
        switch($type)
        {
            case 'M' : $folder = '/models/';break;
            case 'L' : $folder = '/libraries/';break;
            case 'V' : $folder = '/views/';break;
        }
        
        //print WEB_PATH . $folder . $type . $className . '.php';
        require_once WEB_PATH . $folder . $className . '.php';
    }
    
    // request made by server
    $request = $_SERVER['QUERY_STRING'];
    
    // request link as array
    $request_link = explode('&', $request);
    
    $vars = array();
    
    $vars['template'] = $request_link[0]; // first argument, or template
    
    // article id, if is set
    if(count($request_link) > 1){
        list($var, $val) = explode('=', $request_link[1]);
        $vars[$var] = $val; // article id, id=value
    }
    
    // article preview
    if(count($request_link) > 2){
        list($var_, $val_) = explode('=', $request_link[2]);
        $vars[$var_] = $val_; // show code, code= file .....
    }
    
    $file = WEB_PATH . '/controllers/' . 'C' . ucfirst($request_link[0]) . '.php';
    if(file_exists($file)){
        require_once $file;
        $class = ucfirst($request_link[0]);
        if(class_exists($class)){
            $article_controller = new $class;
        }
        else{
            die('Class: ' . $class . ' not exists!!!');
        }
    }else{
        die('File: ' . $file . ' not exists!!!');
    }
    
    $article_controller->getPage($vars);
    
?>